<?php
/**
 * Created by PhpStorm.
 * User: 68008000
 * Date: 2017/12/5
 * Time: 23:59
 */

namespace app\common\model;


use think\Model;
use think\Validate;

class Group extends Model
{
    //连接数据库
    protected $table= 'shop_auth_group';
    protected $pk= 'id';
    public function add($data){
        //添加
        $data['rules'] = implode(',',$data['rules']);
        $result = $this->validate(true)->allowField(true)->save($data);
        if (false == $result) {
            return ['valid' => 0, 'msg' => $this->getError()];
        }
        else {
            return ['valid' => 1, 'msg' => '添加成功！'];
        }
    }
    //编辑
    public function edit($data){
        $id = input('param.id');
        //添加
        $data['rules'] = implode(',',$data['rules']);
        $result = $this->validate(true)->allowField(true)->save($data,['id'=>$id]);
        if (false == $result) {
            return ['valid' => 0, 'msg' => $this->getError()];
        }
        else {
            return ['valid' => 1, 'msg' => '编辑成功！'];
        }
    }
    //删除
    public function del($id){
        $res = Group::destroy($id);
        if ($res){
            return ['valid'=>1,'msg'=>'删除成功'];
        }else{
            return ['valid'=>0,'msg'=>'删除失败'];
        }

    }
}